/**
 * 
 */
package trungsi.gea.photos.client

import org.json.JSONObject
import org.json.JSONArray

import static trungsi.gea.photos.client.JSONConverter.*

/**
 * @author trungsi
 *
 */
public class JSONConverterTests extends GroovyTestCase{

	void testConvertEmptyObject() {
		assert convert(new JSONObject()) == [:]
	}
	
	void testConvertString() {
		assert convert(new JSONObject('{name : "value"}')).name == "value"
	}
	
	void testConvertInt() {
		assert convert(new JSONObject('{name : 10}')).name == 10
	}
	
	void testConvertBoolean() {
		assert convert(new JSONObject('{name : true}')).name == true
	}
	
	void testConvertDouble() {
		assert convert(new JSONObject('{name : 100.123}')).name == 100.123
	}
	
	void testConvertNestedObject() {
		def json = convert(new JSONObject('{obj : {id : 10, name : "test", child : {success : false}}}'))
		assert json.obj.id == 10
		assert json.obj.name == "test"
		assert json.obj.child.success == false
	}
	
	void testConvertEmptyArray() {
		assert convert(new JSONArray()) == []
	}
	
	void testConvertStringArray() {
		def array = convert(new JSONArray('["value1", "value2", "value3"]'))
		assert array == ["value1", "value2", "value3"]
	}
	
	void testConvertIntArray() {
		def array = convert(new JSONArray('[1, 2, 3]'))
		assert array == [1, 2, 3]
	}
	
	void testConvertBooleanArray() {
		def array = convert(new JSONArray('[true, false, true]'))
        assert array == [true, false, true]
	}
	
	void testConvertDoubleArray() {
		def array = convert(new JSONArray('[1.2, 2.34, 4.567]'))
        assert array == [1.2, 2.34, 4.567]
	}
	
	void testConvertMixedArray() {
		def array = convert(new JSONArray('[1.2, "value", false, 10, {id : 100}, [{name : "value2"}, {success : false}]]'))
		assert array == [1.2, "value", false, 10, [id : 100], [[name : "value2"], [success : false]]]
	}
}
